﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MP3Ganizer.BusinessObject.DataToPersist;

namespace MP3Ganizer.BusinessObject.Services.Configuration.Builders
{
    class AlbumsEntreListBuilder : IConfigurationBuilder
    {
        public virtual void SetInfo(DataToPersist.LocalConfiguration config, object newValue, object oldValue)
        {

            var oldString = oldValue as string;
            var newString = newValue as string;
            if (string.IsNullOrEmpty(newString))
                return;
            var newData = config.AlbumsEntireList.FirstOrDefault(item => newString.Equals(item.Name));
            var oldData = config.AlbumsEntireList.FirstOrDefault(item => !string.IsNullOrEmpty(oldString) && oldString.Equals(item.Name));
            if (newData != null)
            {
                if (oldData != null)
                    oldData.OverridenName = newData.Name;
            }
            else
            {
                if (oldData != null)
                    oldData.OverridenName = newString;
                else
                    config.AlbumsEntireList = config.AlbumsEntireList.Concat(new OverridableData[] { new OverridableData(newString) }).ToArray();

            }
        }


        public virtual object GetInfo(DataToPersist.LocalConfiguration localConfiguration)
        {
            return localConfiguration.AlbumsEntireList;
        }


        public virtual void DeleteInfos(DataToPersist.LocalConfiguration config, object p)
        {
            var valueToRemove = p as string;
            config.AlbumsEntireList = config.AlbumsEntireList.Where(item => !item.Name.Equals(valueToRemove)).ToArray();
        }
    }
}
